package com.yyy.modules.web;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import com.yyy.example.entity.User02;
import com.yyy.modules.MsgTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author yyy
 * @Date 2020-09-13 14:21
 * @Email yangyouyuhd@163.com
 */
@RestController
public class HelloController {

    @Resource
    MsgTemplate msgTemplate;



    @GetMapping("/hello")
    public void init() {
        new Thread(()->{
            ThreadUtil.sleep(5,TimeUnit.SECONDS);
            for (int i = 1; i < 11; i++) {
                System.out.println("发送第" + i + "条消息");
                hello(i);
            }
        }).start();
    }

    public Object hello(int age) {
        User02 user = new User02("张三", age, new Date());
        int i = (RandomUtil.randomInt()) % 3;
        if (i == 0) {
            msgTemplate.send("hello", user);
        } else if (i == 1) {
            msgTemplate.send("queue1", user);
        } else {
            msgTemplate.send("queue2", user);

        }
        return user;
    }

}
